      SUBROUTINE ATSHIF(NAME)
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C                                                                      C
C     INSERT A BLANK TO FORM A TWO-CHARACTER ELEMENT NAME, IF NECESSARY
C                                                                      C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
      CHARACTER NAME*(*),ATOM*7,BLANK*1
      DATA ATOM,BLANK/'HCNOPSF',' '/
      IF (NAME(2:2).EQ.BLANK) RETURN
      DO 10 I=1,7
   10 IF (NAME(1:1).EQ.ATOM(I:I)) GOTO 11
      RETURN
   11 DO 20 I=LEN(NAME),3,-1
   20 NAME(I:I)=NAME(I-1:I-1)
      NAME(2:2)=BLANK
      RETURN
      END
      SUBROUTINE BIGINP(NTI,ITYP,ICONV,SOL,NAME,COR,ILEN,NI,NT,XC,ILX)
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C                                                                      C
C     SUBROUTINE BIGINP                                                C
C                                                                      C
C     WRITTEN BY TON RULLMANN, DECEMBER 1983                           C
C                                                                      C
C     BIGINP IS A GENERAL INPUT ROUTINE FOR READING BIGMOL-TYPE DATA   C
C     FROM A BIGMOL- OR MD-FORMATTED FILE.                             C
C     IT IS POSSIBLE TO SKIP THE FIRST OR THE SECOND PART OF THE       C
C     FILE, THE PARTITIONING BEING DEFINED BY A SPECIFIED GROUPNAME.   C
C     TEXTLINES (BEGINNING WITH ", * OR A BLANK) ARE PRINTED           C
C     WHEN ENCOUNTERED WHILE READING IN BIGMOL-FORMAT.                 C
C                                                                      C
C     ARGUMENTS:                                                       C
C      NTI    : TAPE NUMBER OF INPUTFILE                               C
C      ITYP   : ABSOLUTE VALUE DETERMINES FORMAT: 1=BIGMOL, 2=MD       C
C               SIGN DETERMINES WHICH PART OF THE FILE IS TO BE READ   C
C               (IF SOL .NE. BLANK): +=SECOND PART, -=FIRST PART       C
C      ICONV  : DETERMINES CONVERSION OF UNITS                         C
C                  1 = NO CONVERSION                                   C
C                  2 = ANG=> NM                                        C
C                  3 = AU => NM                                        C
C                  4 = AU => ANG                                       C
C                  5 = ANG=> AU                                        C
C               INVERSE CONVERSIONS WHEN ABS(ITYP)=2!                  C
C      SOL    : IF NOT BLANK, SKIP TO/FROM FIRST LINE WITH A GROUPNAME C
C               CONTAINING THE CHARACTERS OF THIS WORD.                C
C      NAME   : ARRAY (LENGTH .GE. ILEN) DELIVERED WITH 'ATOM'NAMES    C
C               (IF FIRST WORD .NE. 'STOP')                            C
C      COR    : ARRAY (LENGTH .GE. 3*ILEN) DELIVERED WITH COORDINATES  C
C               (IF ILEN NOT NEGATIVE)                                 C
C      NI     : DELIVERED WITH NUMBER OF POINTS READ                   C
C               SET TO 0 IF ERROR OCCURS                               C
C               SET TO MINUS NUMBER OF POINTS IF ILEN IS TOO SMALL     C
C      NT     : TAPE NUMBER FOR PRINT OUTPUT                           C
C      XC     : CHARACTER ARRAY DELIVERED WITH SECOND HALF OF EACH     C
C               LINE WHEN READING IN BIGMOL FORMAT (IF ILX>0)          C
C      ILX    : CHARACTER LENGTH OF EACH WORD IN XC                    C
C               CHARACTERS 41 TO 40+ILX ARE STORED                     C
C                                                                      C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
      DIMENSION COR(3,*),CONV(5)
      CHARACTER LINE*80,CHAR*40
      CHARACTER*(*) NAME(*),XC(*),SOL
      CHARACTER QUOTE,STAR,BLANK,UNIT(10)*3,FM(2)*3
      CHARACTER*5 RN,AN,S
      LOGICAL SKIP,FIRST,CHINCH
C
      DATA QUOTE,STAR,BLANK/'"','*',' '/, FM/'BIG','MD '/
      DATA UNIT/'?',2*'NM','ANG','AU','?','ANG',2*'AU','ANG'/
      DATA CONV/1., .1, .0529177, .529177, 1.88973/
C
    1 FORMAT(A)
    2 FORMAT(2A5,3F10.4,A)
    3 FORMAT(I5,2A5,I5,3F8.3)
    4 FORMAT(///' READ COORDINATES FROM TAPE',I2,' IN ',A3,
     +'-FORMAT FROM/TO GROUPNAME ',A5/
     +' CONVERSION FACTOR:',F10.7,'. NEW UNITS ASSUMED: ',A3,
     +'. SKIP: ',L1,'. FIRST: ',L1/)
    5 FORMAT(/1X,I5,' POINTS READ')
    6 FORMAT(I2,A3)
C
      ILE=IABS(ILEN)
      SKIP=.FALSE.
      FIRST=SKIP
      IF (ITYP.EQ.0.OR.ICONV.EQ.0.OR.
     +    IABS(ITYP).GT.2.OR.ICONV.GT.5) CALL ABORT(
     +'F',2,'WRONG ARGUMENTS BIGINP',0,0,0)
      IF (ITYP.LT.0) THEN
        ITYP=-ITYP
        FIRST=.TRUE.
      ENDIF
      IF (SOL.NE.BLANK) SKIP=.TRUE.
      C=CONV(ICONV)
      IF (ITYP.EQ.2) C=1./C
      WRITE (NT,4) NTI,FM(ITYP),SOL,C,UNIT(ICONV+(ITYP-1)*5),SKIP,FIRST
      REWIND (NTI)
      I=0
      IF (ITYP.EQ.1) GOTO 110
      DO 50 J=1,2
      READ (NTI,1,END=1000) LINE
   50 WRITE (NT,1) BLANK//LINE
C
  100 IF (ITYP.EQ.2) GOTO 120
  110 READ (NTI,1,END=140) LINE
      IF (LINE(1:1).EQ.BLANK.OR.LINE(1:1).EQ.QUOTE.OR.
     +    LINE(1:1).EQ.STAR) THEN
        WRITE (NT,1) BLANK//LINE
        GOTO 110
      ENDIF
      READ (LINE,2,ERR=970) AN,RN,X,Y,Z,CHAR
      S=RN
      GOTO 130
C
  120 READ (NTI,3,END=140,ERR=980) J,RN,AN,K,X,Y,Z
      CALL LSHIFC(5,RN)
      S=RN(1:3)
      WRITE (RN,6) MOD(J,100),S
      CALL LSHIFC(5,AN)
      CALL ATSHIF(AN)
C
  130 IF (SKIP.AND.CHINCH(SOL,S)) SKIP=.FALSE.
      IF (SKIP.AND..NOT.FIRST) GOTO 100
      IF (FIRST.AND..NOT.SKIP) GOTO 140
      I=I+1
      IF (I.GT.ILE) THEN
        WRITE (NT,1) ' ** TOO MANY POINTS'
        NI=-I
        RETURN
      ENDIF
      IF (NAME(1).NE.'STOP') NAME(I)=AN//RN
      IF (ILEN.GT.0) THEN
        COR(1,I)=X*C
        COR(2,I)=Y*C
        COR(3,I)=Z*C
      ENDIF
      IF (ILX.NE.0) XC(I)(1:ILX)=CHAR(1:ILX)
      GOTO 100
C
  140 NI=I
      WRITE (NT,5) NI
      RETURN
C
  970 WRITE (NT,1) ' ** LINE NOT IN BIGMOL FORMAT: '//LINE
      GOTO 1000
  980 READ (NTI,1,ERR=981) LINE
  981 WRITE (NT,1) ' ** THIS OR PREVIOUS LINE NOT IN MD FORMAT: '//
     +LINE
 1000 NI=0
      RETURN
      END
      SUBROUTINE CONVRT(NTI,NTO,ITYP,ICONV,K,ISTART,JSTART,SOL,SKIP,QO,
     +                   QH)
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C                                                                      C
C     TON RULLMANN, NOVEMBER 1983                                      C
C                                                                      C
C     CONVERTS BIGMOL-FORMAT TO MD-FORMAT OR VICE VERSA                C
C     AND/OR CONVERTS UNITS.                                           C
C     OPTIONS: ITYP(1/2/3/4), ICONV(1/2/3/4)                           C
C     ITYP =1: BIG => MD                                               C
C          =2: MD  => BIG                                              C
C          =3: BIG => BIG                                              C
C          =4: MD  => MD                                               C
C     ICONV=1: NO CONVERSION                                           C
C          =2: ANGSTROM => NANOMETRE  > V.V. IF ITYPE IS 2 OR 4        C
C          =3: BOHR     => NANOMETRE  >         IDEM                   C
C          =4: BOHR     => ANGSTROM   >         IDEM                   C
C          =5: ANGSTROM = >BOHR       >         IDEM                   C
C     BIGMOL FORMAT: ATOMNAME(A5), GROUPNAME(A5), X, Y, Z              C
C                    OPTIONAL TEXT LINES BEGIN WITH "                  C
C     MD     FORMAT: GROUPNR.(I5), GROUPNAME(A5), ATOMNAME(A5),        C
C                    ATOMNR.(I5), X, Y, Z                              C
C                    FILE BEGINS WITH TEXT LINE, OPTIONAL TEXT LINES   C
C                    BEGINNING WITH " AND LINE WITH NUMBER OF ATOMS.   C
C                                                                      C
C     WHILE CONVERTING FROM MD TO BIG, THE FIRST THREE NON-BLANK       C
C     CHARACTERS OF THE GROUPNAME ARE COMBINED WITH THE GROUPNUMBER.   C
C     ALSO IN THIS CASE A BLANK CARD IS INSERTED BEFORE THE FIRST CARD C
C     WITH GROUPNAME SOL, AND (IF SUCH CARD IS PRESENT) ALSO AT THE ENDC
C     OF THE FILE (BLANK CARDS SEPARATE INTERNAL AND EXTERNAL CHARGES).C
C     IF SKIP, ONLY THE SOLVENT DATA ARE WRITTEN TO THE FILE.          C
C                                                                      C
C     WHILE CONVERTING FROM BIG TO MD ALL TEXT LINES BUT THE FIRST ARE C
C     SKIPPED, JUST AS LINES BEGINNING WITH * OR ' '.                  C
C     IN THIS CASE THE GROUPNUMBER IS RAISED IF A NEW GROUPNAME IS     C
C     ENCOUNTERED.                                                     C
C                                                                      C
C     THE FOLLOWING ARGUMENTS ARE USED WHEN ITYP=1 ONLY:               C
C     K IS THE NUMBER OF ATOMS (OUTPUT AS SECOND LINE).                C
C     ISTART IS THE ATOMNUMBER ASSIGNED TO THE FIRST ATOM.             C
C     JSTART IS THE GROUPNUMBER ASSIGNED TO THE FIRST GROUP.           C
C                                                                      C
C     THE PRESENT VERSION MINIMIZES THE MANIPULATION OF LINES WHICH    C
C     DO NOT CONTAIN COORDINATES, AND DOES NOT READ FROM INPUT.        C
C     VARIOUS OPTIONS ARE DETERMINED BY THE SUBROUTINE ARGUMENTS.      C
C                                                                      C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
      DIMENSION CONV(5),CHA(0:2)
      CHARACTER*5 RNN,RN,AN,QUOTE*1,SOL,BLANK*1
      CHARACTER*80 LINE,CHAR*40,BIG*12,BIG2*14,BIG3*12,MD*17
      LOGICAL FIRST,SKIP
      DATA QUOTE/'"'/, FIRST/.TRUE./
      DATA BIG,MD/'(2A5,3F10.4)', '(I5,2A5,I5,3F8.3)'/
      DATA BIG2/'(2A5,3F10.4,A)'/, BIG3/'(2A5,4F10.4)'/
      DATA BLANK/' '/
      DATA CONV/1., .1, .0529177, .529177, 1.88973/
C
    1 FORMAT(A)
    3 FORMAT(I2,A3)
    4 FORMAT(A1,I5)
C
      REWIND (NTI)
      REWIND (NTO)
      C=CONV(ICONV)
      CHA(0)=QO
      CHA(1)=QH
      CHA(2)=QH
      IF (ITYP.EQ.2.OR.ITYP.EQ.4) C=1./C
      GOTO (10,20,30,40) ITYP
C
   10 CONTINUE
      I=ISTART-1
      J=JSTART-1
      READ (NTI,1,END=99) LINE
      WRITE (NTO,1) LINE
      WRITE (NTO,MD) K
      IF (LINE(1:1).NE.QUOTE) REWIND (NTI)
   12 READ (NTI,1,END=100) LINE
      IF (LINE(1:1).EQ.QUOTE.OR.LINE(1:1).EQ.'*'.OR.
     +    LINE(1:1).EQ.BLANK) GOTO 12
      READ (LINE,BIG,ERR=90) AN,RNN,X,Y,Z
      IF (RNN.NE.RN) J=J+1
      RN=RNN
      I=I+1
      IF (AN(2:2).EQ.BLANK) AN(2:5)=AN(3:5)//BLANK
      WRITE (NTO,MD) J,RN,AN,I,X*C,Y*C,Z*C
      GOTO 12
C
   20 READ (NTI,1,END=99) LINE
      IF (LINE(1:1).NE.QUOTE) THEN
        REWIND (NTI)
        GOTO 22
      ENDIF
      WRITE (NTO,1) LINE
   21 READ (NTI,1,END=99) LINE
      IF (LINE(1:1).EQ.QUOTE) THEN
        WRITE (NTO,1) LINE
        GOTO 21
      ELSE
        READ (LINE,MD,ERR=91) I
        WRITE (NTO,4) QUOTE,I
      ENDIF
   22 READ (NTI,MD,END=23,ERR=92) J,RN,AN,I,X,Y,Z
      CALL LSHIFC(5,RN)
      WRITE (RNN,3) MOD(J,100),RN(1:3)
      CALL LSHIFC(5,AN)
      CALL ATSHIF(AN)
      IF (RN.EQ.SOL.AND.FIRST) THEN
        FIRST=.FALSE.
        K=0
        WRITE (NTO,1)
        IF (SKIP) THEN
          REWIND (NTO)
          READ (NTO,1) LINE
        ENDIF
      ENDIF
      IF (FIRST) THEN
        WRITE (NTO,BIG) AN,RNN,X*C,Y*C,Z*C
      ELSE
        L=MOD(K,3)
        K=K+1
        WRITE (NTO,BIG3) AN,RNN,X*C,Y*C,Z*C,CHA(L)
      ENDIF
      GOTO 22
   23 IF (.NOT.FIRST) WRITE (NTO,1)
      GOTO 100
C
   30 READ (NTI,1,END=100) LINE
      IF (LINE(1:1).EQ.QUOTE.OR.LINE(1:1).EQ.BLANK.OR.
     +    LINE(1:1).EQ.'*') THEN
        WRITE (NTO,1) LINE
      ELSE
        READ (LINE,BIG2,ERR=90) AN,RNN,X,Y,Z,CHAR(1:40)
        WRITE (NTO,BIG2) AN,RNN,X*C,Y*C,Z*C,CHAR(1:40)
      ENDIF
      GOTO 30
C
   40 READ (NTI,1,END=99) LINE
      WRITE (NTO,1) LINE
      READ (NTI,MD,END=99,ERR=91) I
      WRITE (NTO,MD) I
   41 READ (NTI,MD,END=100,ERR=92) J,RN,AN,I,X,Y,Z
      WRITE (NTO,MD) J,RN,AN,I,X*C,Y*C,Z*C
      GOTO 41
C
   90 PRINT 1,BLANK//LINE
      PRINT 1,' LINE NOT IN BIGMOL FORMAT'
      STOP
   91 PRINT 1,' INTEGER (NR. OF ATOMS) EXPECTED'
      STOP
   92 PRINT 1,' LINE NOT IN MD FORMAT'
      STOP
   99 PRINT 1,' PREMATURE END OF FILE'
  100 RETURN
      END
      SUBROUTINE DSTBL1(INAME,JNAME,NI,NJ,ID,JD,IST,JST,
     +                   ICORD,JCORD,SCALE,NT,EQUAL)
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C     SUBROUTINE DSTBL1                                                C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
      REAL ICORD(3*ID/NI,NI),JCORD(3*JD/NJ,NJ)
      CHARACTER*10 INAME(ID/NI,NI),JNAME(JD/NJ,NJ),EQUAL*2
      IF (EQUAL.EQ.'EQ'.AND.NI.NE.NJ) THEN
        CALL ABORT('W',6,'SETS UNEQUAL IN DSTBL1',' ',0,0)
        EQUAL=' '
      ENDIF
      II=1+3*(IST-1)
      JJ=1+3*(JST-1)
      IF=1
      DO 1 JF=1,NJ,17
      JL=MIN0(JF+16,NJ)
      IF (EQUAL.EQ.'EQ') IF=JF
      WRITE (NT,2) (JNAME(JST,J)(1:5),J=JF,JL)
      WRITE (NT,3) (JNAME(JST,J)(6:10),J=JF,JL)
      DO 1 I=IF,NI
    1 WRITE (NT,4) INAME(IST,I),
     +             (SCALE*DIST(ICORD(II,I),JCORD(JJ,J)),J=JF,JL)
    2 FORMAT(//' DISTANCE TABLE'//11X,17(2X,A5))
    3 FORMAT(11X,17(2X,A5))
    4 FORMAT(1X,A10,17F7.3)
      RETURN
      END
      SUBROUTINE DSTBLE(INAME,JNAME,NI,NJ,ID,JD,IST,JST,
     +                   ICORD,JCORD,SCALE,NT,EQUAL)
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C                                                                      C
C     SUBROUTINE DSTBLE                                                C
C                                                                      C
C     DSTBLE WRITES A FORMATTED DISTANCE TABLE TO TAPE NT.             C
C     TWO SETS OF COORDINATES ARE PASSED TO THE ROUTINE, INDICATED BY  C
C     I AND J. NAME CONTAINS THE 'PARTICLE'-NAMES, CORD THEIR CARTESIAN
C     COORDINATES. ID AND JD ARE THE NUMBERS OF 'PARTICLES' IN EACH SET.
C     DISTANCES ARE COMPUTED BETWEEN EVERY I(J)'TH PARTICLE, WITH I(J) C
C     EQUAL TO ID/NI (JD/NJ). IST (JST) DESIGNATES THE NUMBER OF THE   C
C     PARTICLE CONSIDERED IN EACH GROUP OF ID/NI (JD/NJ) PARTICLES.    C
C     THE COMPUTED DISTANCES ARE MULTIPLIED BY SCALE.                  C
C     IF EQUAL IS 'EQ' THE SETS SHOULD BE THE SAME, AND SOME REDUNDANT C
C     INFORMATION IS NOT PRINTED.                                      C
C                                                                      C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
      CHARACTER*2 EQUAL
      IF (NI.LE.NJ) THEN
        CALL DSTBL1(INAME,JNAME,NI,NJ,ID,JD,IST,JST,
     +               ICORD,JCORD,SCALE,NT,EQUAL)
      ELSE
        CALL DSTBL1(JNAME,INAME,NJ,NI,JD,ID,JST,IST,
     +               JCORD,ICORD,SCALE,NT,EQUAL)
      ENDIF
      RETURN
      END
